package com.sise.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sise.pojo.User;
import com.sise.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 彭海霞
 * @since 2020-06-10
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String login(String username, String password, HttpSession session,Model model) {
        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username", username).eq("password", password);
            User user = userService.getOne(queryWrapper);
            session.setAttribute("username", user.getUsername());
            return "index";
        }
        model.addAttribute("flag", "用户名或密码错误");
        return "login";
    }
    @RequestMapping("/user/showUserByUsername")
    public String showUserByUsername(HttpSession session, Model model){
        String username = (String) session.getAttribute("username");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userService.getOne(queryWrapper);
        model.addAttribute("user", user);
        return "user-edit";
    }
    @RequestMapping("/user/updateUser")
    public String updateUser(Long id,String username, String password,MultipartFile img, Model model) {
        System.out.println("1111111111111111111111111yy");
        User user=new User();
        try {
            //文件上传的地址
            String path = "E:/IdeaProjects/crs/src/main/resources/static/upload";
            //获取文件的名称
            final String fileName = UUID.randomUUID() + img.getOriginalFilename();
            //限制文件上传的类型
            String contentType = img.getContentType();
            File file = new File(path, fileName);
            img.transferTo(file);
            user.setId(id).setUsername(username).setPassword(password).setImg("http://localhost:8080/upload/" + fileName);
            System.out.println(user);
            boolean b = userService.updateById(user);
            System.out.println(b);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "index";
    }
}

